<template>
	<view class="container">
		<loading :show="loading_show"></loading>
		<customHead
			title="优惠券"
			isShowBackArrow
			:titlePaddingLeft="30"
			color="#fff"
			:navi_background="`rgba(255, 56, 0, ${opacity})`"
		>
		</customHead>
		<view class="posterImg">
			<image class="couponBanner" :src="couponImg" mode="aspectFill"></image>
		</view>
		
		<view class="couponList" v-if="naviHeight">
			<view class="coupon_item" v-for="(item, index) in couponList" :key="index">
				<view class="coupon_item_main">
					<view class="item_left">
						<view class="icon">
							<text class="couponType" v-if="item.couponType==0" >满减券</text>
							<text class="couponType" v-else-if="item.couponType==2" >积分券</text>
							<text class="couponType" v-else-if="item.couponType==3" >折扣券</text>
							<text class="couponType" v-else-if="item.couponType==1" >口令券</text>
						</view>
						<view class="amount">
							<view class="money" v-if="item.couponType == 0 || item.couponType == 1">
								<text class="text">￥</text>
								<span class="span">{{item.couponDenomination}}</span>
							</view>
							<view class="money" v-else-if="item.couponType == 2">
								<span class="span">{{item.couponDenomination}}</span>
								<text class="text">积分</text>
							</view>
							<view class="money" v-else-if="item.couponType == 3">
								<span class="span">{{item.couponDenomination * 10}}</span>
								<text class="text">折</text>
							</view>
						</view>
						
						<view class="desc" v-if="item.couponType == 0 || item.couponType == 1 || item.couponType == 3">
							<text>满{{item.howMuchFullMoney}}可用</text>
						</view>
					</view>
					<view class="item_right">
						<view class="right_left">
							<text class="desc">{{item.couponName}}</text>
							<text class="date" v-if="item.timeType == 1">{{setDate(item)}}内有效</text>
							<text class="date" v-if="item.timeType == 2">领取后{{item.timeDay}}天内有效</text>
						</view>
						<view class="couponType">
							<text class="text1" v-if="item.goodsType == 1">全部商品可用</text>
							<text class="text1" v-if="item.goodsType == 2">指定商品可用</text>
						</view>
						<view class="avaliable">
							<text class="text2">每人可领取{{item.maxNumber}}张</text>
						</view>
						<view class="bottom">
							<view class="item" @click="clickItem(index)">
								<text>展开详情</text>
								<u-icon name="arrow-down" color="#9d9d9d" size="14"></u-icon>
							</view>
							<view class="right_right">
								<button 
									:disabled="item.receiveStatus" 
									class="button" 
									:class="[item.receiveStatus?'received':'unreceived']"
									:style="{backgroundColor: _temp_themeColor}"
									@click="getCoupon(item)"
								>
									<text >{{item.receiveStatus?'已领取':'领取'}}</text>
								</button>
							</view>
							
						</view>
						
					</view>
				</view>
				<view class="slide_dowm" :class="[couponIndex==index?'show':'hide']">
					<uv-parse :content="item.detail"></uv-parse>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	import {
		searchPlugsCouponsByBusinessId,
		receiveCoupons,
		getApplicationPlugsRecommendedList
	} from "@/tools/request/api.js"
	export default {
		data() {
			return {
				naviHeight: 0,
				couponList: [],
				couponImg: '',
				couponIndex: -1,
				orderPage: {
					page: 1,
					size: 10,
					totalPage: 0,
					hasNextPage: false,
				},
				loading_show: false,
				opacity: 0,
				refresher_show: false
			};
		},
		onShow () {
			this.getcouponList(true);
		},
		onLoad () {
			this.getSwiperList();
		},
		computed: {
			setDate () {
				return function (item) {
					if (!item.startTime || !item.endTime) return; 
					let dateStr = '';
					let startTime = item.startTime.split(' ')[0];
					let endTime = item.endTime.split(' ')[0];
					return startTime + ' 至 ' +  endTime;
				}
			}
		},
		methods: {
			clickItem (index) {
				if (this.couponIndex == index) return this.couponIndex = -1;
				this.couponIndex = index;
			},
			useCoupon () {
				uni.navigateTo({
					url: '/subpack_my/myCoupon/useCoupon/useCoupon'
				})
			},
			gotoMyCoupon () {
				uni.navigateTo({
					url: '/subpack_my/coupon/myCoupon/myCoupon'
				})
			},
			getSwiperList() {
				let data = {
					businessId: this.businessId,
					userServiceToken: this.userServiceToken
				}
				getApplicationPlugsRecommendedList(data).then(res => {
					if (res.code == 200) {
						let list = res.body;
						let swiperList = list.filter(item => item.type == 'coupon');
						if (swiperList.length) {
							this.couponImg = swiperList[0].activityImgUrl;
						}
					}
				})
			},
			// 获取优惠券列表
			getcouponList(clear) {
				this.loading_show = true;
				let postdata = {
					businessId: this.businessId,
					page: this.orderPage.page,
					size: this.orderPage.size,
					userServiceToken: this.userServiceToken
				};
				searchPlugsCouponsByBusinessId(postdata).then((res) => {
					this.loading_show = false;
					if (res.code != 200) return uni.$showMsg();
					this.orderPage.totalPage = res.body.total;
					this.orderPage.hasNextPage = res.body.hasNextPage;
					if (clear) {
						this.couponList = res.body.list;
					} else {
						this.couponList = this.couponList.concat(res.body.list);
					}
					uni.stopPullDownRefresh();
				});
			},
			// 领取优惠券
			getCoupon(item) {
				if (this.accessToken) {
					this.loading_show = true;
					receiveCoupons({ couponsId: item.id })
					.then((res) => {
						this.loading_show = false;
						if (res.code != 200) return uni.$showMsg(res.msg);
						uni.$showMsg('领取成功')
						setTimeout(() => {
							this.gotoMyCoupon();
						}, 500)
					})
					.catch((err)=>{
						console.log(err);
					})
				} else {
					uni.showModal({
						title: '提示',
						content: '您还未登录,登录才能领取哦!',
						success: function (res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/user/login'
								});
							} else if (res.cancel) {
							}
						}
					});
				}
			},
		},
		onReachBottom () {
			if (this.orderPage.hasNextPage) {
				this.orderPage.page++;
				this.getcouponList();
			}
		},
		// 下拉刷新
		onPullDownRefresh () {
			this.orderPage.page = 1;
			this.getcouponList(true);
		},
		
		onPageScroll (e) {
			if (e.scrollTop >= 130) {
				this.opacity = 1;
			} else {
				this.opacity = 0;
			}
		},
		
	}
</script>


<style lang="scss" scoped>
	.container{
		width: 100vw;
		height: 100vh;
		
		.tabs{
			display: flex;
			justify-content: center;
		}
		.posterImg{
			width: 100%;
			
			.couponBanner{
				width: 100%;
				height: 500rpx;
			}
		}
		// .myCoupone_box{
		// 	color: #fff;
		// 	padding-bottom: 20rpx;
		// 	padding-left: 30rpx;
		// 	padding-right: 30rpx;
		// 	display: flex;
		// 	justify-content: space-between;
		// }
		.myCoupone_box{
			width: 100%;
			height: 84rpx;
			padding: 0 20rpx;
			box-sizing: border-box;
			padding-bottom: 20rpx;
			
			.input_box{
				width: 100%;
				height: 100%;
				border-radius: 10rpx;
				display: flex;
				justify-content: space-between; 
				padding-left: 30rpx;
				padding-right: 30rpx;
				align-items: center;
				background-color: rgba(255, 255, 255, 0.1);
				background-color: transparent;
				backdrop-filter: blur(1px);//毛玻璃属性
				
				.text{
					font-size: 26rpx;
					color: #9f9f9f;
				}
			}
		}
		.main_scroll{
			width: 100%;
			height: 100vh;
		}
			
		.couponList{
			width: 100%;
			padding: 20rpx;
			box-sizing: border-box;
			
			.coupon_item{
				width: 100%;
				display: flex;
				flex-direction: column;
				margin-bottom: 20rpx;
				
				.coupon_item_main{
					width: 100%;
					height: 320rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					position: relative;
					overflow: hidden;
					
					.item_left{
						width: 30%;
						height: 100%;
						background-color: #FFE3E5;
						display: flex;
						flex-direction: column;
						justify-content: center;
						align-items: center;
						position: relative;
						border-radius: 20rpx;
						
						.icon{
							position: absolute;
							top: 0;
							left: 0;
							border-top-left-radius: 20rpx;
							border-bottom-right-radius: 20rpx;
							background-color: #FF1414;
							color: #fff;
							font-size: 24rpx;
							padding: 6rpx 20rpx;
						}
						
						.amount{
							
							.money{
								display: flex;
								
								.text{
									color: #FF1414;
									display: flex;
									align-items: flex-end;
									padding-bottom: 8rpx;
								}
								.span{
									font-size: 64rpx;
									font-weight: bold;
									color: #FF1414;
								}
							}
						}
						
						.desc{
							color: #FF1414;
							font-size: 26rpx;
						}
						
						
						
					}
					.item_right{
						height: 100%;
						flex: 1;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						background-color: #fff;
						border-radius: 20rpx;
						padding: 20rpx 20rpx;
						box-sizing: border-box;
						
						.couponType{
							
							.text1{
								border-radius: 14rpx;
								background-color: #FF1414;
								color: #fff;
								font-size: 24rpx;
								padding: 6rpx 10rpx;
							}
							.text2{
								border-radius: 14rpx;
								background-color: $theme-color;
								color: #fff;
								font-size: 24rpx;
								padding: 6rpx 10rpx;
							}
						}
						
						.avaliable{
							
							text{
								font-size: 24rpx;
								color: #b8b8b8;
							}
						}
						
						.right_left{
							display: flex;
							flex-direction: column;
							
							.desc{
								font-size: 34rpx;
								font-weight: bold;
								padding-bottom: 10rpx;
							}
							.date{
								font-size: 24rpx;
								color: #747474;
								font-weight: bold;
							}
						}
						
						
						.bottom{
							display: flex;
							justify-content: space-between;
							align-items: center;
							
							.item{
								display: flex;
								color: #9d9d9d;
								font-size: 26rpx;
							}
							.right_right{
								display: flex;
								align-items: center;
								
								.button{
									width: 130rpx;
									height: 50rpx;
									border-radius: 25rpx;
									font-size: 28rpx;
									color: #fff;
									padding: 0;
								}
								.received{
									background-color: $theme-color;
									opacity: 0.6;
									display: flex;
									justify-content: center;
									align-items: center;
								}
								.unreceived{
									background-color: $theme-color;
									display: flex;
									justify-content: center;
									align-items: center;
								}
								.unreceived:active{
									opacity: 0.6;
								}
							}
						}
						
					}
					
				}
				.slide_dowm{
					width: 100%;
					background-color: #fff;
					font-size: 28rpx;
					color: #7e7e7e;
					padding: 30rpx;
					box-sizing: border-box;
					border-bottom-left-radius: 20rpx;
					border-bottom-right-radius: 20rpx;
					// position: absolute;
					// bottom: 0;
				}
				.show{
					display: block;
					animation-name: show;
					animation-duration: 0.4s;
					animation-fill-mode: forwards;
				}
				.hide{
					display: none;
					animation-name: hide;
					animation-duration: 0.4s;
					animation-fill-mode: forwards;
				}
				@keyframes show {
					from{
						opacity: 0;
					} to {
						opacity: 1;
					}
				}
				@keyframes hide {
					from{
						opacity: 1;
					} to {
						opacity: 0;
					}
				}
				
			}
		}
	}
</style>
